package com.heima.Config;

import com.heima.common.exception.RabbitMQcode;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EorrMQConfig {
    //    声明超时消息交换机
    @Bean
    public DirectExchange directExchange()
    {
        return new DirectExchange(RabbitMQcode.ErrorExchangeName);
    }

    //    声明超时消息存储队列
    @Bean
    public Queue errorQueue()
    {
        return new Queue(RabbitMQcode.ErrorExchangeQueueName);
    }

    //    绑定交换机和队列
    @Bean
    public Binding Errormassgbinding(Queue errorQueue, DirectExchange directExchange)
    {
        return BindingBuilder.bind(errorQueue).to(directExchange).with(RabbitMQcode.ErrorRoutingKey);
    }

    //    消息失败处理机制  发送给errorexchange
    @Bean
    public MessageRecoverer messageRecoverer(RabbitTemplate rabbitTemplate)
    {
        return new RepublishMessageRecoverer(rabbitTemplate,RabbitMQcode.ErrorExchangeName,RabbitMQcode.ErrorRoutingKey);
    }
}
